package com.baidu.qianfan.controller;

import com.baidu.qianfan.entity.ERNIE4ResponseBody;
import com.baidu.qianfan.entity.ERNIESpeed128KBody;
import com.baidu.qianfan.entity.StableDiffusionXLResponseBody;
import com.baidu.qianfan.service.ERNIE4;
import com.baidu.qianfan.service.ERNIESpeed128K;
import com.baidu.qianfan.service.StableDiffusionXL;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
@RequestMapping("/ai")
@CrossOrigin
@Slf4j
public class AiController {

    @Autowired
    private ERNIE4 ernie4;
    @Autowired
    private StableDiffusionXL stableDiffusionXL;
    @Autowired
    private ERNIESpeed128K ernieSpeed128K;

    @GetMapping("/ERNIE4")
    public ERNIE4ResponseBody ERNIE4Api() throws Exception {
        String text = "今天是什么日子？";
        ERNIE4ResponseBody responseBody = ernie4.extractedERNIE4(text);
        if (responseBody.getError_code() == null) {
            System.out.println(responseBody.toString());
            System.out.println(responseBody.getResult());
        }else {
            log.info("错误码error_code是: {}", responseBody.getError_code());
            log.info("错误信息error_msg是: {}", responseBody.getError_msg());
        }
        return responseBody;
    }

    @GetMapping("/ERNIESpeed128K")
    public ERNIESpeed128KBody ERNIESpeed128KApi() throws Exception {
        String text = "你可以做什么？";
        ERNIESpeed128KBody responseBody = ernieSpeed128K.extractedERNIESpeed128K(text);
        if (responseBody.getError_code() == null) {
            System.out.println(responseBody.toString());
            System.out.println(responseBody.getResult());
        }else {
            log.info("错误码error_code是: {}", responseBody.getError_code());
            log.info("错误信息error_msg是: {}", responseBody.getError_msg());
        }
        return responseBody;
    }
    
    @GetMapping("/stableDiffusionXL")
    public StableDiffusionXLResponseBody stableDiffusionXLApi() throws Exception {
        String text = "唯美高清动漫风景图";
        StableDiffusionXLResponseBody responseBody = stableDiffusionXL.extracted(text);
        if (responseBody.getError_code() == null) {
            List<StableDiffusionXLResponseBody.ImageObject> data = responseBody.getData();
            for (int i = 0; i < data.size(); i++) {
                String b64Image = responseBody.getData().get(i).getB64_image();
            }
        } else {
            System.out.println("错误码error_code是: " + responseBody.getError_code());
            System.out.println("错误信息error_msg是: " + responseBody.getError_msg());
        }
        return responseBody;
    }
}
